package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 前台分页查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> page(int page,int pageSize){

        //获取当前用户id
        Long userId = BaseContext.getCurrentId();

        log.info("page = {},pageSize = {}",page,pageSize);

        //分页构造器对象
        Page<Orders> pageInfo =new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage =new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();


        //添加查询条件
        queryWrapper.eq(userId!=null,Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        //分页查询
        orderService.page(pageInfo,queryWrapper);

        //对象拷贝( records是要排除的字段)
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");

        List<Orders> records=pageInfo.getRecords();

        //获取OrderDto对象
        List<OrdersDto> ordersDtoList=records.stream().map((item) ->{

            OrdersDto ordersDto=new OrdersDto();
            //对象拷贝
            BeanUtils.copyProperties(item,ordersDto);

            //获取orders表的od,并从orderDetail表中查询orderid为id的数据
            Long ordersId = item.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId,ordersId);
            List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper1);

            ordersDto.setOrderDetailArrayList(orderDetails);
            return ordersDto;

        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 分页查询后台订单数据
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageList(int page, int pageSize, Long number, String beginTime,String endTime){
        log.info("page={},pageSize={},number={},beginTime={},endTime={}",page,pageSize,number,beginTime,endTime);

        //分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(number!=null,Orders::getNumber,number);
        queryWrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper.le(beginTime!=null,Orders::getOrderTime,endTime);
        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo,queryWrapper);


        return R.success(pageInfo);
    }

    /**
     * 修改后台订单状态
     * @param order
     * @return
     */
    @PutMapping()
    public R<Orders> editOrderDetail(@RequestBody  Orders order){

        Orders orders = orderService.getById(order.getId());

        //修改订单状态
        if (order.getStatus()<=5){
            orders.setStatus(order.getStatus());
        }

        orderService.updateById(orders);


     return R.success(orders);
    }

}